// pages/pay/pay.js
const app = getApp();
const http = require("../../config/api.js");
const user = require("../../utils/login.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.payParam)
    var payParam = JSON.parse(decodeURIComponent(options.payParam)) 
    console.log(payParam)
    wx.requestPayment({
      timeStamp: payParam.timeStamp.toString(),
      nonceStr: payParam.nonceStr,
      package: payParam.package,
      signType: payParam.signType,
      paySign: payParam.paySign,
      success(res) {
        wx.setStorage({
          key: 'isVIP',
          data: true,
        })
        wx.switchTab({
          url: '../index/index',
        })
      },
      fail(res) {
        console.log(res)
        if (JSON.parse(wx.getStorageSync('VIP')).orderStatus != 0) {
          wx.setStorage({
            key: 'isVIP',
            data: true,
          })
        } else {
          wx.setStorage({
            key: 'isVIP',
            data: false,
          })
        }
        wx.switchTab({
          url: '../index/index',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let time = Math.round(new Date().getTime());
    let log = {
      //日志类型：unite.通用业务；visit.访问数据；click.点击数据；order.订购数据；error.错误日志
      logType: "visit",
      msg: JSON.stringify({
        mac: app.globalData.userId,
        page: "xcxpay",
        pageName: "梨园行支付",
        time: time,
        appVersionName: "小程序",
        item: 65,
        nodeCode: "001000",
        project: "lyhad",
        userId: app.globalData.memberId,
        deviceId: app.globalData.userId,
        uuid: user.getUuid(),
        pageLevel: "1"
      })
    }
    this.uniteLog(log)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 日志
  uniteLog(data) {
    http.lhxRequest("api_log", data).then(res => {
      if (res.code === 10000000) {
        console.log("日志")
      }
    })
  }
})